import {Component, OnDestroy, OnInit, ViewChild, ViewEncapsulation} from '@angular/core';
import {MineService} from "@mine/mine.service";
import {ToolsService} from "@shared/service/tools/tools.service";
import {SeedInfiniteScroll} from "@shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component";
import {ActivatedRoute, Params, Router} from "@angular/router";
import {Subject} from "rxjs/Subject";

@Component({
  selector: 'notice',
  templateUrl: 'notice.component.html',
  styleUrls: ['notice.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class NoticeComponent implements OnInit, OnDestroy {
  @ViewChild(SeedInfiniteScroll) infiniteScroll;
  public ngUnsubscribe: Subject<void> = new Subject<void>();
  public notices = [];
  public dataStore = {
    pageTitle: '',
    noticeType: ''
  };
  public stateStore = {
    haveData: true
  };
  private _params = {
    page: 1,
    rows: 10
  };

  constructor(public userService: MineService,
              public tools: ToolsService,
              private _route: ActivatedRoute,
              private _router: Router) {
    this._route.params.subscribe((params: Params) => {
      this.dataStore.noticeType = params['noticeType'];
    });
  }

  /**
   * @description 获取我的评论
   * @author ouyqx-691
   * @date 2017-12-15
   */
  getNotices() {
    const noticeType = this.dataStore.noticeType;
    switch (noticeType) {
      case 'comments':
        this.dataStore.pageTitle = '我的评论';
        this.getMyComments();
        break;
      case 'at':
        this.dataStore.pageTitle = '@我的';
        this.getCallMe();
        break;
      case 'thumbs-up':
        this.dataStore.pageTitle = '我的点赞';
        this.getMyThumbsUp();
        break;
    }
  }

  /**
   * @description 获取我的评论
   * @author ouyqx-691
   * @date 2018-1-31
   */
  getMyComments() {
    this.tools.showLoading();
    this.userService.getMyComments(this._params).takeUntil(this.ngUnsubscribe).subscribe(res => {
      this.tools.hideLoading();
      if (res.result === '0') {
        this._params.page++;
        const notices = res.data.list;
        if (!res.data.hasNextPage) {
          if (notices.length === 0) {
            this.stateStore.haveData = false;
          }
          this.infiniteScroll.enable(false);
        }
        this.notices = [...this.notices, ...notices];
      }
    });
  }

  /**
   * @description 获取@我的
   * @author ouyqx-691
   * @date 2018-1-31
   */
  getCallMe() {
    this.userService.getAtMe(this._params).takeUntil(this.ngUnsubscribe).subscribe(res => {
      if (res.result === '0') {
        this._params.page++;
        const notices = res.data.list;
        if (!res.data.hasNextPage) {
          if (notices.length === 0) {
            this.stateStore.haveData = false;
          }
          this.infiniteScroll.enable(false);
        }
        this.notices = [...this.notices, ...notices];
      }
    });
  }

  /**
   * @description 获取我的点赞
   * @author ouyqx-691
   * @date 2018-1-31
   */
  getMyThumbsUp() {
    this.userService.getDiggMe(this._params).takeUntil(this.ngUnsubscribe).subscribe(res => {
      if (res.result === '0') {
        this._params.page++;
        const notices = res.data.list;
        if (!res.data.hasNextPage) {
          if (notices.length === 0) {
            this.stateStore.haveData = false;
          }
          this.infiniteScroll.enable(false);
        }
        this.notices = [...this.notices, ...notices];
      }
    });
  }


  /**
   * @description 前往帖子详情
   * @author ouyqx-691
   * @date 2018-1-31
   */
  goToTargetPage(notice) {
    this._router.navigate(['/feed/detail', notice.feedId]);
  }

  ngOnInit() {
    this.getNotices();
  }
  ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }
}
